
require('./config/db')

const Event = require('./utils/event')


const Menu = require('./models/menuSchema')
const Depts = require('./models/deptSchema')
const Roles = require('./models/roleSchema')
const User = require('./models/userSchema')

// 菜单
const menuList = require('./config/menus.json')
// 权限
const depts = require('./config/depts.json')
// 身份
const roles = require('./config/roles.json')
// user
const users = require('./config/users.json')

  /*
  查询数据库, 如果表是空的就初始化数据
  */
  ; (async () => {
    Event.on('dbConnect', async () => {
      console.log('数据库连接成功, 开始初始化数据')

      for (const data of menuList) {
        await Menu.create(data)
      }
      console.log('菜单初始化完成')

      for (const data of depts) {
        await Depts.create(data)
      }
      console.log('权限初始化完成')

      for (const data of roles) {
        await Roles.create(data)
      }
      console.log('角色初始化完成')
      for (const data of users) {
        await User.create(data)
      }
      console.log('User 初始化完成')

      process.exit()
    })

  })();
